/*
 * @Description: 退回
 * @Author: Rfan
 * @Date: 2022-11-03 10:57:33
 * @LastEditTime: 2022-11-16 20:31:41
 */

import { useEffect, useMemo, useState } from 'react';
import { Button, Popover } from 'antd';
import { circulationArchivesMethod } from '@/api/archives/scattered';
import { fetchRepoListByType } from '@/api/archivesType';
import { useLevelTableContext } from '../../contexts/LevelTableContext';
import type { IRepo } from '@/interfaces/archiveType';
import CirculationDialog from '@/components/LevelTable/componentes/dialogs/ArchiveCirculation';

const ReturnArchives = (props: { list: any }) => {
  const { list } = props;
  const { selectItems, baseInfo } = useLevelTableContext();

  // popover显示状态
  const [visible, setVisible] = useState(false);
  // 目标库列表
  const [repoList, setRepoList] = useState<IRepo[]>([]);
  // 弹窗显示状态
  const [dialogVisible, setDialogVisible] = useState(false);

  const [flowData, setFlowData] = useState(null);

  /**
   * @description: 监听档案类型变化请求库列表
   */
  useEffect(() => {
    fetchRepoListByType(baseInfo.atType, baseInfo.archiveTypeId).then((res: any) => {
      const { data } = res;
      setRepoList(data.filter((repo: IRepo) => repo.createStatus));
    });
  }, [baseInfo.archiveTypeId]);

  /**
   * @description: 退回操作
   * @param {IRepo} props
   * @return {*}
   */
  const returnFunc = (props: IRepo): void => {
    const { atCode, atType, level, dbName } = baseInfo;
    circulationArchivesMethod({
      coordinate: { atCode, atType, level },
      fromDb: dbName,
      movingEntryInfo: { entryIdList: selectItems, searchCondition: '' },
      toDb: props.name
    });
  };

  /**
   * @description: 监听popover弹层显示状态方法
   * state为false时关闭。打开使用按钮控制
   * @param {boolean} visibleState
   * @return {*}
   */
  const handleOpenChange = (visibleState: boolean): void => {
    !visibleState && setVisible(visibleState);
  };

  const showReturnDialog = (item: any) => {
    setFlowData(item);
    returnFunc(item);
  };

  /**
   * @description: 弹层内容
   */
  const repoPopover = (
    <>
      {repoList
        .filter((repo) => repo.name !== baseInfo.dbName)
        .map((item) => (
          <div key={item.id}>
            <Button
              type="text"
              onClick={() => {
                showReturnDialog(item);
              }}
            >
              {item.description}
            </Button>
          </div>
        ))}
    </>
  );

  return (
    <>
      <Popover
        visible={visible}
        content={repoPopover}
        trigger={['click']}
        placement="bottom"
        onVisibleChange={handleOpenChange}
      >
        <Button
          disabled={selectItems.length === 0}
          onClick={() => {
            setVisible(true);
          }}
        >
          退回
        </Button>
      </Popover>
      {useMemo(
        () => (
          <CirculationDialog
            data={{ ...flowData, list }}
            title="退回"
            visible={dialogVisible}
            onClose={() => {
              setDialogVisible(false);
            }}
          />
        ),
        [dialogVisible]
      )}
    </>
  );
};
export default ReturnArchives;
